package tour.spring.bc.enums;

import java.util.ArrayList;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public enum UserRole {
	ROLE_ADMIN {
		@Override
		public UserDetails getUserDetails(String login, String password) {

			return new org.springframework.security.core.userdetails.User(
					login, password, enabled, accountNonExpired,
					credentialsNonExpired, accountNonLocked,
					new ArrayList<GrantedAuthority>() {
						{
							add(new GrantedAuthorityImpl(name()));
						}
					});
		}
	},

	ROLE_USER {
		@Override
		public UserDetails getUserDetails(String login, String password) {

			return new org.springframework.security.core.userdetails.User(
					login, password, enabled, accountNonExpired,
					credentialsNonExpired, accountNonLocked,
					new ArrayList<GrantedAuthority>() {
						{
							add(new GrantedAuthorityImpl(name()));
						}
					});
		}
		
		
	};

	public static UserRole getByEnumName(String enumName) {
		for (UserRole userRole : UserRole.values()) {
			if (enumName.equals(userRole.name())) {
				return userRole;
			}
		}
		return null;
	}

	public abstract UserDetails getUserDetails(String login, String password);
	
	private static boolean enabled = true;
	private static boolean accountNonExpired = true;
	private static boolean credentialsNonExpired = true;
	private static boolean accountNonLocked = true;

}
